const {
  reqGetAllComment,
  reqGetCommentByUserId
} = require("@/api/comment")
const {
  modal
} = require("@/utils/extendApi")
const {
  reqRemoveCommentById
} = require("@/api/comment")
const {
  ComponentWithStore
} = require("mobx-miniprogram-bindings")
const {
  default: userStore
} = require("@/store/userStore")
ComponentWithStore({
  data: {
    outerActive: 'all',
    innerActive: 'a',
    isLoding: false,
    isFinished: false,
    total: '',
    allCommentQueryParams: {
      page: 1,
      size: 10,
      level: 'a' // a:好评，b:中评，c:差评
    },
    commentQueryParamsByUser: {
      userId: '',
      page: 1,
      size: 10
    },
    commentList: []
  },
  storeBindings: {
    store: userStore,
    fields: ['userInfo']
  },
  methods: {
    outerSwitch(event) {
      const name = event.detail.name
      if (name === 'user') {
        this.setData({
          'commentQueryParamsByUser.page': 1,
          'commentQueryParamsByUser.size': 10,
          outerActive: 'user',
          isFinished: false
        })
        this.refreshCommentByUserId()
      } else {
        this.setData({
          allCommentQueryParams: {
            page: 1,
            size: 10,
            level: 'a' // a:好评，b:中评，c:差评
          },
          innerActive: 'a',
          outerActive: 'all',
          isFinished: false
        })
        this.refreshAllComment()
      }
    },
    innerSwitch(event) {
      this.setData({
        'allCommentQueryParams.level': event.detail.name,
        innerActive: event.detail.name,
        'allCommentQueryParams.page': 1,
        isFinished: false
      })
      this.refreshAllComment()
    },
    async removeComment(event) {
      const confirm = await modal({
        content: '你确定删除吗？'
      })
      if (!confirm) {
        return
      }
      await reqRemoveCommentById(event.detail)
      if (this.data.outerActive === 'user') {
        this.data.commentQueryParamsByUser.page = 1
        this.setData({
          isFinished: false
        })
        this.refreshCommentByUserId()
      } else {
        this.data.allCommentQueryParams.page = 1
        this.setData({
          isFinished: false
        })
        this.refreshAllComment()
      }
    },
    handlePicStr(picStr) {
      if (picStr === '') {
        return []
      }
      return picStr.split(";")
    },
    async refreshAllComment() {
      const res = await reqGetAllComment(this.data.allCommentQueryParams)
      for (let i = 0; i < res.data.records.length; i++) {
        res.data.records[i].picList = this.handlePicStr(res.data.records[i].pic)
      }
      this.setData({
        commentList: res.data.records,
        total: res.data.total
      })
    },

    async refreshCommentByUserId() {
      const res = await reqGetCommentByUserId(this.data.commentQueryParamsByUser)
      for (let i = 0; i < res.data.records.length; i++) {
        res.data.records[i].picList = this.handlePicStr(res.data.records[i].pic)
      }
      this.setData({
        commentList: res.data.records,
        total: res.data.total
      })
    },
    back() {
      wx.navigateBack()
    },
    async onPullDownRefresh() {
      if (this.data.isLoading) {
        return
      }
      if (this.data.outerActive === 'user') {
        this.setData({
          'commentQueryParamsByUser.page': 1,
          isFinished: false
        })
        this.data.isLoading = true
        await this.refreshCommentByUserId()
        this.data.isLoading = false
        await wx.stopPullDownRefresh()
      } else {
        this.setData({
          'allCommentQueryParams.page': 1,
          isFinished: false
        })
        this.data.isLoading = true
        await this.refreshAllComment()
        this.data.isLoading = false
        await wx.stopPullDownRefresh()
      }
    },
    async onReachBottom() {
      console.log('到底')
      if (this.data.isLoading) return
      if (this.data.total <= this.data.commentList.length) {
        this.setData({
          isFinished: true
        })
        return
      }
      if (this.data.outerActive === 'user') {
        this.setData({
          'commentQueryParamsByUser.page': this.data.commentQueryParamsByUser.page + 1,
          isLoading: true
        })
        const res = await reqGetCommentByUserId(this.data.commentQueryParamsByUser)
        this.data.commentList.push(...res.data.records)
        this.setData({
          commentList: this.data.commentList,
          total: res.data.total,
          isLoading: false
        })
      } else {
        this.setData({
          'allCommentQueryParams.page': this.data.allCommentQueryParams.page + 1,
          isLoading: true
        })
        const res = await reqGetAllComment(this.data.allCommentQueryParams)
        this.data.commentList.push(...res.data.records)
        this.setData({
          commentList: this.data.commentList,
          total: res.data.total,
          isLoading: false
        })
      }
    }
  },
  pageLifetimes: {
    show() {
      this.data.commentQueryParamsByUser.userId = this.data.userInfo.id
      this.refreshAllComment()
    }
  }
})